﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DevExpress.MailClient.Win.Classes
{
    public class FilesManagement : PRO_DOCS.BLL.WIN.Templates.MasterAppClas
    {

        public bool FILES_ADDED
        { get; set; }

        bool _cONTINUE_UPLOADING;
        bool CONTINUE_UPLOADING
        {
            get { return _cONTINUE_UPLOADING; }
            set
            {
                _cONTINUE_UPLOADING = value;
            }
        }

        List<string> CURRENT_ARCHIVES_COPYING_FOLDERS
        {
            get;
            set;
        }
        PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT _cURRENT_SELECTED_ARCHIVE_DEPARTMENT;
        PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT CURRENT_SELECTED_ARCHIVE_DEPARTMENT
        {
            get { return _cURRENT_SELECTED_ARCHIVE_DEPARTMENT; }
            set
            {
                _cURRENT_SELECTED_ARCHIVE_DEPARTMENT = value;
                CURRENT_ARCHIVES_COPYING_FOLDERS = new List<string>();
                CONTINUE_UPLOADING = false;

                if (_cURRENT_SELECTED_ARCHIVE_DEPARTMENT.ARCHIVE_DEPARTMENT_ID != 0)
                {


                    List<PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHE> aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHES = _cURRENT_SELECTED_ARCHIVE_DEPARTMENT.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHy.ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHEs.ToList();

                    for (int i = 0; i < aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHES.Count; i++)
                    {
                        if (new DirectoryInfo(aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHES[i].ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH).Exists)
                        {
                            CONTINUE_UPLOADING = true;
                            CURRENT_ARCHIVES_COPYING_FOLDERS.Add(aRCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATHES[i].ARCHIVE_DEPARTMENTS_STORAGES_HEIRARCHY_PHYSICAL_PATH);
                        }
                    }

                    if (!CONTINUE_UPLOADING)
                    {
                        XtraMessageBox.Show
                            (
                                string.Format("The storage media for the \"{0}\" department was not found.{1}Please make sure that you are connected to the network or contact your network administrator", _cURRENT_SELECTED_ARCHIVE_DEPARTMENT.ARCHIVE_DEPARTMENT_NAME, Environment.NewLine),
                                "Storage Media was not found",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning
                            );
                    }
                }
            }
        }

        public void COPY_SAVE_AS_NEW_FILE
            (
                PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE aRCHIVE_DEPARTMENT_FILE,
                PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE CURRENT_SELECTED_DESTINATION_ARCHIVE_DEPARTMENTS_FOLDER
            )
        {

            DevExpress.MailClient.Win.Data.UploadedPhysicalFiles uploadedPhysicalFile = new Data.UploadedPhysicalFiles();
            uploadedPhysicalFile.FileId = Guid.NewGuid();

            CURRENT_SELECTED_ARCHIVE_DEPARTMENT = aRCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE.ARCHIVE_DEPARTMENT;
            bool originalFileFound = false;
            bool fileUploaded = false;
            //1. Copy the file to the Folder

            List<PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_HISTORY> aRCHIVE_DEPARTMENT_FILE_HISTORies = aRCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_HISTORies.ToList();
            aRCHIVE_DEPARTMENT_FILE_HISTORies = aRCHIVE_DEPARTMENT_FILE_HISTORies.OrderBy(k => k.ARCHIVE_DEPARTMENT_FILE_HISTORY_UPLOADED_DATE.Value).ToList();
            PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_HISTORY oLD_ARCHIVE_DEPARTMENT_FILE_HISTORY = aRCHIVE_DEPARTMENT_FILE_HISTORies[aRCHIVE_DEPARTMENT_FILE_HISTORies.Count - 1];

            {
                //the new fileId to be named with
                string newCreatedPhysicalFileName = uploadedPhysicalFile.FileId.ToString();
                //the original file path
                FileInfo originalFile = null;
                //a. copy the file
                for (int fc = 0; fc < CURRENT_ARCHIVES_COPYING_FOLDERS.Count; fc++)
                {
                    try
                    {
                        originalFile = new FileInfo(Path.Combine(CURRENT_ARCHIVES_COPYING_FOLDERS[fc], oLD_ARCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_ID.ToString()));
                        if (originalFile.Exists)
                        {
                            uploadedPhysicalFile.FileInfo = originalFile;

                            originalFileFound = true;
                        }
                        break;
                    }
                    catch (Exception ex)
                    {
                    }
                }
                if (originalFileFound)
                {
                    for (int fc = 0; fc < CURRENT_ARCHIVES_COPYING_FOLDERS.Count; fc++)
                    {
                        try
                        {
                            originalFile.CopyTo(Path.Combine(CURRENT_ARCHIVES_COPYING_FOLDERS[fc], newCreatedPhysicalFileName));
                            fileUploaded = true;
                            break;
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
                if (fileUploaded)
                {
                    FILES_ADDED = true;
                    //2. ADD the file table
                    PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE newly_ARCHIVE_DEPARTMENT_FILE = new PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE();
                    newly_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_ID = Guid.NewGuid();
                    //--------->aRCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_HISTORY_ID = aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_ID;
                    newly_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_ID = CURRENT_SELECTED_DESTINATION_ARCHIVE_DEPARTMENTS_FOLDER.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_ID;
                    newly_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_DISPLAY_NAME = aRCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_DISPLAY_NAME;//originalFile.Name.Replace(originalFile.Extension, string.Empty);
                    //---------->get the document type
                    newly_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_DOCUMENT_TYPE_ID = aRCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_DOCUMENT_TYPE_ID.Value;
                    newly_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_GENERATED_BARCODE_VALUE = PRO_DOCS.BLL.WIN.Utilities.FileType.GenerateRandomBarcode();
                    newly_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_CREATION_DATE = DateTime.Now;
                    newly_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_IS_LOCKED = false;
                    newly_ARCHIVE_DEPARTMENT_FILE.IS_DELETED = false;

                    PRO_DOCSDatabaseDataContext.proc_ARCHIVE_DEPARTMENT_FILESInsert
                        (
                        newly_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_ID,
                        newly_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_ID.Value,
                        newly_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_CREATION_DATE.Value,
                        PRO_DOCS.BLL.WIN.Authentication.LOGGED_IN_EMPLOYEE_DETAILS.LOGIN_USER.LOGIN_USER_ID,
                        newly_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_DISPLAY_NAME,
                        newly_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_DOCUMENT_TYPE_ID.Value,
                        newly_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_GENERATED_BARCODE_VALUE,
                        newly_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_DISPLAY_NOTES,
                        newly_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_IS_LOCKED.Value,
                        newly_ARCHIVE_DEPARTMENT_FILE.IS_DELETED.Value
                        );

                    //1. ADD The file history
                    PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_HISTORY aRCHIVE_DEPARTMENT_FILE_HISTORY = new PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_HISTORY();
                    aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_ID = uploadedPhysicalFile.FileId;
                    aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_ID = newly_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_ID;
                    //aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_ID = Guid.NewGuid();
                    aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_CHANGING_TYPE_ID = 1;
                    aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_ORIGINAL_NAME = oLD_ARCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_ORIGINAL_NAME;
                    aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_EXTENSION = oLD_ARCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_EXTENSION;
                    aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_UPLOADED_BY_USER_ID = PRO_DOCS.BLL.WIN.Authentication.LOGGED_IN_EMPLOYEE_DETAILS.LOGIN_USER.LOGIN_USER_ID;
                    aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_FILE_SIZE = oLD_ARCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_FILE_SIZE.Value;
                    aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_UPLOADED_DATE = DateTime.Now;

                    aRCHIVE_DEPARTMENT_FILE_HISTORY.IS_DELETED = false;


                    PRO_DOCSDatabaseDataContext.proc_ARCHIVE_DEPARTMENT_FILE_HISTORYInsert
                    (
                    aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_ID,
                    aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_ID.Value,
                    aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_CHANGING_TYPE_ID.Value,
                    aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_ORIGINAL_NAME,
                    aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_EXTENSION,
                    aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_UPLOADED_BY_USER_ID.Value,
                    aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_UPLOADED_DATE.Value,
                    aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_FILE_SIZE.Value,
                    aRCHIVE_DEPARTMENT_FILE_HISTORY.ARCHIVE_DEPARTMENT_FILE_HISTORY_NOTES,
                    aRCHIVE_DEPARTMENT_FILE_HISTORY.IS_DELETED.Value);


                }
            }
        }
    }
}
